import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: 'Home'
    }
  },
  {
    path: '/search',
    // name: 'Search',
    component: () => import('@/views/Search.vue'),
    meta: {
      title: 'Search'
    },
    children: [{
      path: '',
      name: 'SearchArticle',
      component: () => import('@/views/Search/SearchArticle.vue'),
      meta: {
        title: 'Search'
      },
    },
    {
      path: 'author',
      name: 'SearchAuthor',
      component: () => import('@/views/Search/SearchAuthor.vue'),
      meta: {
        title: 'Search'
      },
    }]
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import(/* webpackChunkName: "about" */ '@/views/Test.vue'),
    meta: {
      title: 'Test'
    }
  },
  {
    path: '/article/:id',
    name: 'Article',
    component: () => import('@/views/Article/index.vue'),
    meta: {
      title: 'Article'
    }
  },
  {
    path: '/author/:id',
    name: 'Author',
    component: () => import('@/views/Author/index.vue'),
    meta: {
      title: 'Author'
    }
  },
  {
    path: '/affiliation/:id',
    name: 'Affiliation',
    component: () => import('@/views/Affiliation/index.vue'),
    meta: {
      title: 'Affiliation'
    }
  },
  {
    path: '/review',
    name: 'Reviewer',
    component: () => import('@/views/Review/Review.vue'),
    meta: {
      title: 'Reviewer'
    }
  }
]


const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})


export default router
